package com.jiangyao.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiangyao.common.exception.ServerException;
import com.jiangyao.dao.IEvaluationDao;
import com.jiangyao.model.dto.backend.EvaluationDTO;
import com.jiangyao.model.dto.backend.EvaluationListPageDTO;
import com.jiangyao.model.entity.Evaluation;
import com.jiangyao.model.vo.backend.EvaluationListVO;
import com.jiangyao.service.EvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 评价 服务实现类
 * </p>
 *
 * @author lijing
 * @since 2020-08-30
 */
@Service
public class EvaluationServiceImpl extends BaseServiceImpl<IEvaluationDao, Evaluation> implements EvaluationService {

	@Autowired
	private IEvaluationDao evaluationDao;

	@Override
	public Page<EvaluationListVO> getListPage(EvaluationListPageDTO listPageDTO) {
		Page<EvaluationListVO> page = new Page<>(listPageDTO.getPageNo(), listPageDTO.getPageSize());
		evaluationDao.getListPageForBackend(page, listPageDTO);
		return page;
	}

	@Override
	public Page<EvaluationListVO> getReplyListPage(EvaluationListPageDTO listPageDTO) {
		Page<EvaluationListVO> page = new Page<>(listPageDTO.getPageNo(), listPageDTO.getPageSize());
		evaluationDao.getReplyListPage(page, listPageDTO);
		return page;
	}

	@Override
	public void reply(EvaluationDTO replyDTO) {
		Evaluation evaluation = this.getById(replyDTO.getId());
		if (evaluation == null) {
			throw new ServerException(100, "评论不存在");
		}

		evaluation.setId(null);
		evaluation.setType(1);
		evaluation.setUserId(getLoginAdminUserId());
		evaluation.setReleteId(replyDTO.getId());
		evaluation.setContent(replyDTO.getContent());
		this.save(evaluation);
	}

	@Override
	public void editStatus(EvaluationDTO editDTO) {
		UpdateWrapper<Evaluation> uw = new UpdateWrapper<>();
		uw.in("id", editDTO.getIdList());
		uw.set("status", editDTO.getStatus());
		this.update(uw);
	}
}
